Skip to content

Conversation

@o-santi
Copy link

@o-santi o-santi commented Nov 14, 2025

What is the current behavior?

Adds python type generation support, piggy backing off of @ryanpeach's #808 pull request. The main difference between his approach is that I added a lot more structure by first creating the "AST", then serializing it through the serialize() method, instead of in place constructing strings everywhere.

Fixes #795.

Additional context

The idea is that the normal Public{table} class should be used to parse the result of select queries, while the Insert and Update objects should be passed directly into the .insert and .update postgrest methods. As such, they needed to be TypedDicts instead of BaseModels, for that's what the postgrest library expects in those functions.

This may change in the future, specially in a v3. Ideally, I'd love to be able to infer the type of the arguments for the function from the query builder itself, like TS already does, but I'm afraid that python's type checker is not strong enough to do that. I think a frankenstein can be stitched together from Literals and @overloads but not sure how scalable and user friendly that would be. I will certainly research more about it before doing a v3 rewrite. For now, this approach suffices.

@coveralls
Copy link

coveralls commented Nov 14, 2025

Pull Request Test Coverage Report for Build 19441158062

Details

  • 336 of 346 (97.11%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.7%) to 83.632%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/server/server.ts 0 3 0.0%
src/server/templates/python.ts 313 316 99.05%
src/server/routes/generators/python.ts 21 25 84.0%
Totals Coverage Status
Change from base Build 19279040591: 0.7%
Covered Lines: 6260
Relevant Lines: 7346

💛 - Coveralls

Copy link
Member

@avallete avallete left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. In terms of release, we could make a canary release from this PR, setup some client-side tests (even if basics just to ensure no regressions and working types e2e) on the supabase-py repo.

(A bit like it's done for the TS types here: https://github.com/supabase/supabase-js/blob/master/packages/core/postgrest-js/test/resource-embedding.test.ts#L133-L152)

Idea would be to use the canary image to generate types for a test database, and have some basics runtime + types testing to see if it work as expected before getting this merged to production. WDYT ?

@silentworks
Copy link

After a small modification to the formatForPyClassName function, these are the pydantic models generated for my schemas that can be found here https://github.com/silentworks/flask-notes/tree/main/supabase/migrations. This is looking good but it currently doesn't generate table names starting with _ correctly, but I don't think this is much of an issue as developers shouldn't be naming table beginning with _ in the first place and adding a disclaimer about this should be enough.

model.py

@silentworks
Copy link

@o-santi please remember to add the necessary changes in the package.json scripts section and also update server.ts with the python template function you created here.

@silentworks
Copy link

This looks good to me. I've tested it and the types it return looks correct.

Copy link
Member

@avallete avallete left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one last comment on the PR but nothing blocking.

Great work on this one ! Love the AST approach 👍

}: GeneratorMetadata): string => {
const ctx = new PythonContext(types, columns, schemas)
const py_tables = tables
.filter((table) => schemas.some((schema) => schema.name === table.schema))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note

Was still curious about why this was necessary and it's indeed needed but only for the types that aren't filtered at introspection level. Everything else should already be filtered based on user requested schemas.

Made a PR with a proposal and test coverage for it there based on your branch: #1016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generating types in python format

5 participants